import React from "react";
import Relay from "react-relay";

import {Spin,message} from 'antd';

import LogicTable from './components/logic_tableDetail';
import InoutDetailModal from  "./components/logic_inoutDetailModal";

import GetSendRecordsMutation from './mutaiton/GetSendRecords';
import GetSendDetails from "./mutaiton/getSendDetails";
import AcceptGoodsOrder from './mutaiton/acceptGoodsOrder';

import "./css/mian.css";

import {Button} from 'fly';

export default class A extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isLoading:true,  // 页面loading
            TableDataList:[],  // 主table列表数据

            detailModalVis:false,  // 查看编辑弹框的vis
            detailModalData:{},     // 查看编辑页面的data数据
        };
        // this.props.orderId  TD20170607110000020147
        this.orderId = this.props.orderId;          //订单编号
        this.orderStatus = this.props.orderStatus;  //是否收发货  0收  1发

        this.openInoutDetailModal = this.openInoutDetailModal.bind(this);   // 打开收发货详情弹框法法
        this.InSuccess = this.InSuccess.bind(this);
        this.httpRequest = this.httpRequest.bind(this);
    }

    componentDidMount(){
        this.httpRequest();
    }

    // request 请求
    httpRequest(){
        Relay.Store.commitUpdate(
            new GetSendRecordsMutation({
                orderId:this.orderId,
                acceptStatus:""   // 1 传就是收货，发货传空
            }),{
                onFailure:(err)=>{
                    message.error("网络连接失败，请联系系统管理员");
                },
                onSuccess:(res)=>{

                    const d = JSON.parse(JSON.stringify(res.getSendRecords.getSendRecords));

                    console.log(d);

                    d.sendRecordss.forEach((item,index)=>{
                        const d = new Date(item.sendDate);
                        item.key = index;
                        item.sendDate = d.toLocaleString();
                        item.receiveStatus = item.receiveStatus == "0" ? "未收货" : "已收货"
                    });


                    if(d.errorCode == "0"){
                        this.setState({
                            TableDataList:d.sendRecordss
                        })
                    }else{
                        message.error("数据请求失败");
                    }
                    this.setState({isLoading:false});
                }
            }
        )
    }

    // 请求数据并显示查看编辑弹框
    openInoutDetailModal(z,t,i){
        // 显示查看编辑弹框
        // 请求数据
        const r = this.refs;

        if(r.InoutDetailModal){
            r.InoutDetailModal.setState({
                vis:true,
                curItem:t
            })
        }

        Relay.Store.commitUpdate(
            new GetSendDetails({
                orderId:this.orderId,
                // acceptStatus:this.orderStatus == "1" ? "" : "1"  ,   // 1 就是 收货
                recordId:t.sid
            }),{
                onFailure:(err)=>{
                    message.error("网络连接失败，请联系系统管理员");
                },
                onSuccess:(res)=>{
                    const d = JSON.parse(JSON.stringify(res.getSendDetails.getSendDetails));
                    if(d.errorCode == "0"){
                        const obj = {};
                        obj.sendResponse = d.sendResponse;
                        obj.LogisticsMsgList = d.LogisticsMsgList;

                        this.setState({detailModalData:obj})
                    }else{
                        if(d.errorCode == "500"){ message.error("服务器错误，请联系系统管理员"); } else {
                            message.info("网络错误，请刷新页面或重试");
                        }
                    }
                }
            }
        )
    }

    //发货完成
    InSuccess(t){
        Relay.Store.commitUpdate(
            new AcceptGoodsOrder({
                orderId:this.orderId,
                goodsInfo:t.sid
            }),{
                onFailure:(err)=>{
                    message.error("网络连接失败，请联系系统管理员");
                },
                onSuccess:(res)=>{
                    if(res.acceptGoodsOrder.acceptGoodsOrder.errorCode == "0"){
                        message.success("收货成功");
                        this.httpRequest();
                    }else{
                        message.error("确认收货失败");
                    }
                }
            }
        )
    }

    render() {
        const {isLoading,TableDataList,detailModalVis,detailModalData} = this.state;
        return(
            <Spin spinning={isLoading}>
                <div className="order_inout_record" style={isLoading ? {visibility:"hidden"} : {}}>
                    <LogicTable
                        data={TableDataList}
                        orderStatus={this.orderStatus}
                        openInoutDetailModal={this.openInoutDetailModal}
                    />

                    {/*显示编辑弹框*/}
                    <InoutDetailModal
                        ref="InoutDetailModal"
                        orderStatus={this.orderStatus}
                        vis={detailModalVis}
                        data={detailModalData}
                        InSuccess={this.InSuccess}
                    />

                </div>
            </Spin>
        )
    }
}